package com.library.controller;

import com.library.entity.Reader;
import com.library.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/reader")
public class ReaderController {

    @Autowired
    private ReaderService readerService;

    @GetMapping("/register")
    public String registerForm(Model model) {
        model.addAttribute("reader", new Reader());
        return "reader/register";
    }

    @PostMapping("/register")
    public String register(@ModelAttribute Reader reader, Model model) {
        boolean success = readerService.register(reader);
        if (success) {
            return "redirect:/reader/login";
        } else {
            model.addAttribute("error", "注册失败，用户名或邮箱已存在");
            return "reader/register";
        }
    }

    @GetMapping("/login")
    public String loginForm() {
        return "reader/login";
    }

    @PostMapping("/login")
    public String login(@RequestParam String username,
                       @RequestParam String password,
                       HttpSession session,
                       Model model) {
        Reader reader = readerService.login(username, password);
        if (reader != null) {
            session.setAttribute("reader", reader);
            return "redirect:/reader/dashboard";
        } else {
            model.addAttribute("error", "用户名或密码错误");
            return "reader/login";
        }
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("reader");
        return "redirect:/";
    }

    @GetMapping("/dashboard")
    public String dashboard(HttpSession session, Model model) {
        Reader reader = (Reader) session.getAttribute("reader");
        if (reader == null) {
            return "redirect:/reader/login";
        }
        model.addAttribute("reader", reader);
        return "reader/dashboard";
    }

    @GetMapping("/profile")
    public String profile(HttpSession session, Model model) {
        Reader reader = (Reader) session.getAttribute("reader");
        if (reader == null) {
            return "redirect:/reader/login";
        }
        model.addAttribute("reader", reader);
        return "reader/profile";
    }

    @PostMapping("/updateProfile")
    public String updateProfile(@ModelAttribute Reader reader, HttpSession session) {
        Reader currentReader = (Reader) session.getAttribute("reader");
        if (currentReader == null) {
            return "redirect:/reader/login";
        }
        
        reader.setId(currentReader.getId());
        reader.setUsername(currentReader.getUsername());
        readerService.updateReader(reader);
        
        // 更新session中的用户信息
        session.setAttribute("reader", readerService.getReaderById(currentReader.getId()));
        return "redirect:/reader/profile";
    }
}